package com.sbck.cs3.MyClass;

import android.annotation.SuppressLint;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import androidx.annotation.Nullable;

import com.sbck.cs3.MyGlobal;
import com.sbck.cs3.UNITINFO;

import java.util.ArrayList;

public class MyOpenHelper extends SQLiteOpenHelper {
    public MyOpenHelper(@Nullable Context context) {
        super(context, "cs3.db", null, 1);
        // 第二个参数: 数据库名称
        // 第三个参数: 默认NULL
        // 第四个参数: 数据库版本号
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
    //当数据库文件第一次创建会调用改函数,可以创建表和初始化数据
        sqLiteDatabase.execSQL("create table deviceinfo (_id integer primary key autoincrement, Region varchar(20), BH  varchar(20)  , Name varchar(20) ,  DTU integer , JD, double, WD double, AdTel varchar(20) ,IsLinePower varchar(20) )");
        //sqlite id这一列一般都 _id
        System.out.println("onCreate 被调用");

        sqLiteDatabase.execSQL("create table setting (_id integer primary key autoincrement, ServerIP varchar(20), ServerIPLocal  varchar(20)  , Name varchar(20) ,  IsWifi varchar(20) ,  UserTel varchar(20) )");
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
        //表结构修改添加修改字段
        //添加表
        //sqLiteDatabase.execSQL(("alter table info add age integer"));
        System.out.println("onUpgrade 被调用");
        System.out.println("on Upgrade oldVersion:= " + i + " newVersion:= " + i1);


    }

    @Override
    public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    //    super.onDowngrade(db, oldVersion, newVersion);
        System.out.println("onDowngrade");
    }

    public boolean IsExist()
    {
        int dtu = Integer.valueOf(MyGlobal.unitinfo.DTU.get());
        SQLiteDatabase database = getReadableDatabase();
        String sql = "select * from deviceinfo where DTU = " + String.valueOf(dtu);
        Cursor cursor = database.rawQuery(sql, null);

        boolean result = cursor.getCount() > 0;

        database.close();
        return result;
    }
    public void Add()
    {
        UNITINFO unitinfo = MyGlobal.unitinfo;
        if(unitinfo.IsNew.get()) {
            SQLiteDatabase database = getReadableDatabase();

            int dtu = Integer.valueOf(unitinfo.DTU.get());
            String sql = String.format("insert  into deviceinfo (BH,Name,DTU, IsLinePower) values('%s','%s','%d','%s')",
                    unitinfo.BH.get(),unitinfo.Name.get(), dtu, unitinfo.IsLinePower.get());
            database.execSQL(sql);
            database.close();
            unitinfo.IsNew.set(false);
        }
    }

    public void Update()
    {
        UNITINFO unitinfo = MyGlobal.unitinfo;

        //创建(数据库文件不存在)或打开(数据库文件存在)一个数据库,可读写
        SQLiteDatabase database = getReadableDatabase();

        int dtu = Integer.valueOf(unitinfo.DTU.get());

        String sql = String.format("update  deviceinfo set BH = '%s', Name = '%s', IsLinePower = '%s'    where DTU = '%d'", unitinfo.BH.get(), unitinfo.Name.get(), unitinfo.IsLinePower.get() ,dtu);
        database.execSQL(sql);

        database.close();
    }

    @SuppressLint("Range")
    public int GetAll()
    {
        MyGlobal.unitinfoArrayList = new ArrayList<UNITINFO>();

        SQLiteDatabase database = getReadableDatabase();
        String sql = "select * from deviceinfo";
        Cursor cursor = database.rawQuery(sql, null);


        if( cursor.getCount() <= 0)
        {
            return  0;
        }
        else {
            while (cursor.moveToNext()) {

                UNITINFO unitinfo = new UNITINFO();
                unitinfo.IsNew.set(false);
                unitinfo.BH.set(cursor.getString(cursor.getColumnIndex("BH")));
                unitinfo.Name.set(cursor.getString(cursor.getColumnIndex("Name")));
                unitinfo.DTU.set(String.valueOf(cursor.getInt(cursor.getColumnIndex("DTU"))));
                unitinfo.IsLinePower.set(Boolean.valueOf(cursor.getString(cursor.getColumnIndex("IsLinePower"))));

                MyGlobal.unitinfoArrayList.add(unitinfo);

            }
        }

        cursor.close();
        database.close();
        return MyGlobal.unitinfoArrayList.size();
    }

    public void Delete()
    {
        SQLiteDatabase database = getReadableDatabase();
        String sql = String.format("delete from deviceinfo where DTU = %d",Integer.valueOf(MyGlobal.unitinfo.DTU.get().trim()));
        database.execSQL(sql);
        database.close();
    }

    public void Save_InitSetting()
    {
        SQLiteDatabase database = getReadableDatabase();

        MyGlobal.setting.IsWifi.set(false);
        MyGlobal.setting.UserTelData=2988649767L;
        MyGlobal.setting.UserTelDataStr.set("2988649767");
        MyGlobal.setting.ServerIp.set("150.129.138.47");
        MyGlobal.setting.ServerIpLocalHost.set("192.168.1.1");

        String sql = String.format("insert  into setting (ServerIP,ServerIPLocal , IsWifi , UserTel) values('%s','%s','%s','%s')",
                MyGlobal.setting.ServerIp.get(), MyGlobal.setting.ServerIpLocalHost.get(), MyGlobal.setting.IsWifi.get(), String.valueOf(MyGlobal.setting.UserTelDataStr.get()));
        database.execSQL(sql);
        database.close();
    }
    @SuppressLint("Range")
    public void Get_Setting()
    {
        SQLiteDatabase database = getReadableDatabase();
        String sql = "select * from setting";
        Cursor cursor = database.rawQuery(sql, null);


        if( cursor.getCount() <= 0)
        {
            Save_InitSetting();
        }
        else {
            while (cursor.moveToNext()) {

                MyGlobal.setting.ServerIp.set(cursor.getString(cursor.getColumnIndex("ServerIP")));
                MyGlobal.setting.ServerIpLocalHost.set(cursor.getString(cursor.getColumnIndex("ServerIPLocal")));
                MyGlobal.setting.IsWifi.set(Boolean.valueOf( cursor.getString(cursor.getColumnIndex("IsWifi"))));
                MyGlobal.setting.UserTelDataStr.set(cursor.getString(cursor.getColumnIndex("UserTel")));
                MyGlobal.setting.UserTelData = Long.valueOf(MyGlobal.setting.UserTelDataStr.get());
                break;
            }
        }
    }

    public void Update_Setting()
    {
        SQLiteDatabase database = getReadableDatabase();
        String sql = String.format("update  setting set ServerIP = '%s', ServerIPLocal = '%s', IsWifi = '%s' , UserTel = '%s' ", MyGlobal.setting.ServerIp.get(), MyGlobal.setting.ServerIpLocalHost.get(), MyGlobal.setting.IsWifi.get(),MyGlobal.setting.UserTelDataStr.get());
        database.execSQL(sql);

        database.close();
    }
}
